package cn.sangyy.org.controller;

import cn.sangyy.auth.annotation.Permission;
import cn.sangyy.basic.utils.AjaxResult;
import cn.sangyy.basic.utils.PageList;
import cn.sangyy.org.domain.Employee;
import cn.sangyy.org.query.EmpQuery;
import cn.sangyy.org.service.IEmployeeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author SangYY
 * @date: 2023/05/16/16:05
 */
@RequestMapping("/emp")
@RestController
@Api(tags = "员工管理")
@Permission(name = "员工管理",desc = "员工管理权限")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
    * @author Sangyy
    * 查询所有员工
    */
    @GetMapping("/getAllEmp")
    @ApiOperation(value = "获取所有的员工")
    @Permission(name = "获取所有的员工",desc = "获取所有的员工权限")
    public AjaxResult getAllEmp(){
        List<Employee> employees = employeeService.getAllEmp();
        return AjaxResult.me().setResultObj(employees);
    }

    /**
    * @author Sangyy
    * 分页+高级查询
    */
    @ApiOperation(value = "分页加条件查询员工")
    @Permission(name = "分页加条件查询员工信息",desc = "分页加条件查询员工信息权限")
    @PostMapping()
    public AjaxResult getPageListByKeyword(@RequestBody EmpQuery empQuery){
        try {
            PageList<Employee> pageList = employeeService.getPageList(empQuery);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("操作失败!"+e.getMessage());
        }
    }

    /**
    * @author Sangyy
    * 新增/修改
    */
    @ApiOperation(value = "新增/修改")
    @Permission(name = "新增/修改",desc = "新增/修改员工信息权限")
    @PostMapping("/insertOrUpdate")
    public AjaxResult insertOrUpdate(@RequestBody Employee employee){
        Long id = employee.getId();
        if(id == null){
            employeeService.insert(employee);
        }else {
            employeeService.updateById(employee);
        }
        return AjaxResult.me();
    }
}
